import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.swing.*;
import java.awt.*;

public class mainPanel {
    JFrame frame = new JFrame("计算器");
    JPanel outPanel = new JPanel(),//输出窗口
            inPanel = new JPanel();//输入窗口
    JTextField finField = new JTextField();
    JButton[] buttons = new JButton[20];
    int addPoint = 0;//是否可以添加小数点 0-数字首位 1-可以添加 2-已添加小数点
    boolean addChar = false;//是否可添加运算符
    boolean flag = false;//当前结果是否为初始值
    double finResult;

    mainPanel() {
        frame.setSize(300, 480);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setLayout(null);
        frame.setResizable(false);
        createOutPanel();
        createInPanel();
        frame.add(outPanel);
        frame.add(inPanel);
        frame.setVisible(true);
    }

    void createOutPanel() {
        outPanel.setBounds(5, 15, frame.getWidth() - 25, frame.getHeight() / 8);
        outPanel.setLayout(new GridLayout());
        outPanel.setBackground(Color.WHITE);
        finField.setMargin(new Insets(0, 5, 0, 5));
        finField.setFont(new Font("宋体", Font.PLAIN, 18));
        finField.setHorizontalAlignment(4);
        outPanel.add(finField);
    }

    void createInPanel() {
        inPanel.setBounds(5, outPanel.getHeight() + 25, frame.getWidth() - 25, frame.getHeight() - outPanel.getHeight() - 80);
        inPanel.setBackground(Color.WHITE);
        inPanel.setLayout(new GridLayout(0, 4, 2, 2));
        createButton();
        for (int i = 0; i < buttons.length; i++) {
            buttons[i].setBackground(null);
            inPanel.add(buttons[i]);
        }
    }

    void createButton() {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("js");
        buttons[0] = new JButton("%");
        buttons[1] = new JButton("CE");
        buttons[1].addActionListener(e -> {
            finField.setText("");
        });
        buttons[2] = new JButton("C");
        buttons[2].addActionListener(e -> {
            finField.setText("");
        });
        buttons[3] = new JButton("回格");
        buttons[3].addActionListener(e -> {
            String now = finField.getText();
            if (now.length() > 0) {
                String fin_str = now.substring(0, now.length() - 1);
                finField.setText(fin_str);
            }
        });
        buttons[4] = new JButton("7");
        buttons[4].addActionListener(e -> {
            addChar = true;
            finField.setText(finField.getText() + 7);
            if (addPoint == 0) addPoint++;
        });
        buttons[5] = new JButton("8");
        buttons[5].addActionListener(e -> {
            addChar = true;
            finField.setText(finField.getText() + 8);
            if (addPoint == 0) addPoint++;
        });
        buttons[6] = new JButton("9");
        buttons[6].addActionListener(e -> {
            addChar = true;
            finField.setText(finField.getText() + 9);
            if (addPoint == 0) addPoint++;
        });
        buttons[7] = new JButton("X");
        buttons[7].addActionListener(e -> {
            if (addChar) {
                addChar = false;
                finField.setText(finField.getText() + "*");
            }
            addPoint = 0;
        });
        buttons[8] = new JButton("4");
        buttons[8].addActionListener(e -> {
            addChar = true;
            finField.setText(finField.getText() + 4);
            if (addPoint == 0) addPoint++;
        });
        buttons[9] = new JButton("5");
        buttons[9].addActionListener(e -> {
            addChar = true;
            finField.setText(finField.getText() + 5);
            if (addPoint == 0) addPoint++;
        });
        buttons[10] = new JButton("6");
        buttons[10].addActionListener(e -> {
            addChar = true;
            finField.setText(finField.getText() + 6);
            if (addPoint == 0) addPoint++;
        });
        buttons[11] = new JButton("-");
        buttons[11].addActionListener(e -> {
            if (addChar) {
                addChar = false;
                finField.setText(finField.getText() + "-");
            }
            addPoint = 0;
        });
        buttons[12] = new JButton("1");
        buttons[12].addActionListener(e -> {
            addChar = true;
            finField.setText(finField.getText() + 1);
            if (addPoint == 0) addPoint++;
        });
        buttons[13] = new JButton("2");
        buttons[13].addActionListener(e -> {
            addChar = true;
            finField.setText(finField.getText() + 2);
            if (addPoint == 0) addPoint++;
        });
        buttons[14] = new JButton("3");
        buttons[14].addActionListener(e -> {
            addChar = true;
            finField.setText(finField.getText() + 3);
            if (addPoint == 0) addPoint++;
        });
        buttons[15] = new JButton("+");
        buttons[15].addActionListener(e -> {
            if (addChar) {
                finField.setText(finField.getText() + "+");
                addChar = false;
            }
            addPoint = 0;
        });
        buttons[16] = new JButton("/");
        buttons[16].addActionListener(e -> {
            if (addChar) {
                finField.setText(finField.getText() + "/");
                addChar = false;
            }
            addPoint = 0;
        });
        buttons[17] = new JButton("0");
        buttons[17].addActionListener(e -> {
            addChar = true;
            finField.setText(finField.getText() + 0);
            if (addPoint == 0) addPoint++;
        });
        buttons[18] = new JButton(".");
        buttons[18].addActionListener(e -> {
            if (addPoint == 1) {
                addPoint++;
                finField.setText(finField.getText() + ".");
            }
        });
        buttons[19] = new JButton("=");
        buttons[19].addActionListener(e -> {
            String tmpStr = finField.getText();
            try {
                finField.setText(String.valueOf(engine.eval(tmpStr)));
            } catch (ScriptException scriptException) {
                scriptException.printStackTrace();
            }
            addPoint = 1;
            addChar = true;
            flag = false;
        });
    }

    public static void main(String[] args) {
        mainPanel fun = new mainPanel();
    }
}
